//
//  YZMUser.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/21.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMUser.h"

#define sYZMCurrentUserDefault       @"YZMCurrentUserDefault"        /*当前用户*/

#define sYZMUserDetailArchive    @"YZMUserDetailArchive"
#define sYZMUserAssetsArchive    @"YZMUserAssetsArchive"
#define sYZMUserBankCardArchive  @"YZMUserBankCardArchive"
#define sYZMUserBankCardListArchive @"YZMUserBankCardListArchive"

@implementation YZMUser

- (void)encodeWithCoder:(NSCoder *)aCoder //将属性进行编码
{
    [aCoder encodeObject:self.detail forKey:sYZMUserDetailArchive];
    [aCoder encodeObject:self.assets forKey:sYZMUserAssetsArchive];
//    [aCoder encodeObject:self.bankCard forKey:sYZMUserBankCardArchive];
    [aCoder encodeObject:self.bankCardList forKey:sYZMUserBankCardListArchive];
}


- (id)initWithCoder:(NSCoder *)aDecoder //将属性进行解码
{
    self = [super init];
    
    if (self) {
        
        self.detail = [aDecoder decodeObjectForKey:sYZMUserDetailArchive];
        self.assets = [aDecoder decodeObjectForKey:sYZMUserAssetsArchive];
//        self.bankCard = [aDecoder decodeObjectForKey:sYZMUserBankCardArchive];
        self.bankCardList = [aDecoder decodeObjectForKey:sYZMUserBankCardListArchive];
    }
    
    return self;
}


- (void)createEncoder{
    
    NSMutableData *data = [NSMutableData data];
    //创建一个归档类
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:sYZMCurrentUserDefault];
    [archiver finishEncoding];
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:sYZMCurrentUserDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)createEncoderNil{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: sYZMCurrentUserDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (id)createDecoder{
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:sYZMCurrentUserDefault];
    
    if (data && data.length != 0) {
        
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        
        YZMUser *instance = [unarchiver decodeObjectForKey:sYZMCurrentUserDefault];
        [unarchiver finishDecoding];
        
        return instance;
    }
    
    return nil;
}

@end
